/**
 * Copyright 2018 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.dream.inspect.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.plugins.Page;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.modules.business.entity.SuperviseStandardEntity;
import com.dream.inspect.modules.business.entity.SuperviseTargetEntity;
import com.dream.inspect.modules.sys.entity.SysRoleEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dream.inspect.common.annotation.DataFilter;
import com.dream.inspect.common.utils.Constant;
import com.dream.inspect.modules.dream.pojo.SysDeptFormMap;
import com.dream.inspect.modules.sys.dao.SysDeptDao;
import com.dream.inspect.modules.sys.entity.SysDeptEntity;
import com.dream.inspect.modules.sys.service.SysDeptService;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {
	
	@Resource
	private SysDeptDao sysDeptDao;
	
	@Override
	@DataFilter(subDept = true, user = false)
	public List<SysDeptEntity> queryList(Map<String, Object> params){
		long parentId=0;
		List<SysDeptEntity> deptList =  sysDeptDao.queryAllDeptList();
//		List<SysDeptEntity> deptList =
//			this.selectList(
//					new EntityWrapper<SysDeptEntity>()
//
//							.eq("PARENT_ID",parentId)
//							.or()
//							.gt("PARENT_ID",parentId)
//			.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));

		Map<Long, SysDeptEntity> map = new HashMap<Long, SysDeptEntity>();
		for (SysDeptEntity t : deptList) {
			map.put(t.getDeptId(), t);
		}

		for(SysDeptEntity sysDeptEntity : deptList){
			SysDeptEntity parentDeptEntity = map.get(sysDeptEntity.getParentId());
//			SysDeptEntity parentDeptEntity =  this.selectById(sysDeptEntity.getParentId());
			if(parentDeptEntity != null){
				sysDeptEntity.setParentName(parentDeptEntity.getName());
			}
		}
		return deptList;
	}

	@Override
	public PageUtils queryListByPage(Map<String, Object> params) {
		String fullName = (String) params.get("fullName");

		Page<SysDeptEntity> page = this.selectPage(
				new Query<SysDeptEntity>(params).getPage(),
				new EntityWrapper<SysDeptEntity>()
						.like(StringUtils.isNotBlank(fullName), "full_name", fullName)
						.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER))
		);
		return  new PageUtils(page);
	}


	@Override
	public PageUtils findDeptQuestionList(Map<String, Object> params) {
		Page<SysDeptFormMap> page = selectDeptQuestionThisPage(new Query<SysDeptFormMap>(params),
				new Query<SysDeptFormMap>(params).getPage()
		);

		return new PageUtils(page);
	}

	private Page<SysDeptFormMap> selectDeptQuestionThisPage(Map<String ,Object> map, Page<SysDeptFormMap> page) {
		page.setRecords(sysDeptDao.queryDeptQuestionList(map,page));
		return page;
	}


	@Override
	public List<Long> queryDetpIdList(Long parentId) {
		return baseMapper.queryDetpIdList(parentId);
	}

	@Override
	public List<Long> getSubDeptIdList(Long deptId){
		//部门及子部门ID列表
		List<Long> deptIdList = new ArrayList<>();

		//获取子部门ID
		List<Long> subIdList = queryDetpIdList(deptId);
		getDeptTreeList(subIdList, deptIdList);

		return deptIdList;
	}

	/**
	 * 递归
	 */
	private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
		for(Long deptId : subIdList){
			List<Long> list = queryDetpIdList(deptId);
			if(list.size() > 0){
				getDeptTreeList(list, deptIdList);
			}

			deptIdList.add(deptId);
		}
	}

	@Override
	public List<SysDeptFormMap> findTreeDept(Map<String, Object> params) {
		if(!params.containsKey("flag")){
			params.put("flag", "0");
		}
		List<SysDeptFormMap> deptIdList = sysDeptDao.findTreeDept(params);
		return deptIdList;
	}

	@Override
	public List<SysDeptFormMap> findNextDepts(Map<String, Object> map) {
		List<SysDeptFormMap> deptIdList = sysDeptDao.findNextDepts(map);
		return deptIdList;
	}

	@Override
	public List<SysDeptFormMap> findDeptByParentCode(Map<String, Object> map) {
		List<SysDeptFormMap> deptIdList = sysDeptDao.findDeptByParentCode(map);
		return deptIdList;
	}

	@Override
	public List<SysDeptFormMap> findNextDeptByParentCode(Map<String, Object> map) {
		List<SysDeptFormMap> deptIdList = sysDeptDao.findNextDeptByParentCode(map);
		return deptIdList;
	}

	@Override
	public List<SysDeptFormMap> findAllOtherDept(Map<String, Object> map) {
		List<SysDeptFormMap> deptIdList = sysDeptDao.findAllOtherDept(map);
		return deptIdList;
	}

	@Override
	public List<Map<String, Object>> findAllOtherDeptTree(Map<String, Object> map) {
		long time1= System.currentTimeMillis();
		List<SysDeptFormMap> deptIdList = sysDeptDao.findAllOtherDept(map);
		long time2= System.currentTimeMillis();
		//System.err.println(time2 - time1);  数据库查询需要5s左右
		Map<String, Object> map1 = new HashMap<String, Object>();
		for (Map<String, Object> t : deptIdList) {
			List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
			t.put("children", children);
			map1.put(t.get("code").toString(), t);
        }
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < deptIdList.size(); i++) {
        	Map<String, Object> temp2 = deptIdList.get(i);
            if (null != map1.get(temp2.get("parentCode").toString())) {
                ((List<Map<String, Object>>)(((Map<String, Object>) map1.get(temp2.get("parentCode").toString())).get("children"))).add(temp2);
                continue;
            } else {
                result.add(temp2);
            }
        }
        // tree结构只要7毫秒
        // System.err.println(System.currentTimeMillis() - time2);
		return result;
	}
	
}
